import api from "../../utils/api";

// pages/formApprove/formApprove.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    detailList: [],
    approRole: "",
    visitorDetailList: [],
    requestId: 0,
    processList: [],
    showDialog: false,
    approFlag: true, // 默认true已审，解决已审状态接口未请求完成时，底下操作按钮闪现问题
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    if (options.applyType == '3' || options.applyType == '4') {
      console.log(options.applyType)
      switch (options.applyType){
        case '3':
          wx.redirectTo({
            url: '/pages/formMonitor/formMonitor?id=' + options.id + '&approRole=' + options.approRole + '&source=' + options.source,
          })
          break;
        case '4':
          wx.redirectTo({
            url: '/pages/formPowerOn/formPowerOn?id=' + options.id + '&approRole=' + options.approRole + '&source=' + options.source,
          })
          break;
        default:
          break;
      }
    } else {
      this.setData({
        show: true,
        requestId: options.id,
        approRole: options.approRole
      })
      if (options.source) {
        wx.setStorage({
          data: options.source,
          key: 'source',
        })
      }
      this.getRequestDetail()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  //点击放大图片
  previewPic: function (e) {
    let imgUrls = [e.currentTarget.dataset.url]
    wx.previewImage({
      urls: imgUrls, //需要预览的图片http链接列表，注意是数组
      current: '', // 当前显示图片的http链接，默认是第一个
      success: function (res) {},
      fail: function (res) {},
      complete: function (res) {},
    })
  },

  goToReject: function (e) {
    let requestId = this.data.requestId
    wx.navigateTo({
      url: "/pages/formReject/formReject?requestId=" + requestId
    })
  },

  getRequestDetail: function (e) {
    let that = this

    wx.request({
      url: api.formRequestDetail + this.data.requestId,
      data: {
        workno: wx.getStorageSync("workNo")
      },
      method: "GET",
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {
        if (res.data.state == 1) {
          let tempList = res.data.data
          tempList.pertypeName = that.getPeopleType(res.data.data.pertype)
          tempList.applyApproveList.some(item => {
            if (item.approRole == that.data.approRole) {
              // console.log(item.approRole + ',当前角色' + that.data.approRole + '已审批')
              that.setData({ approFlag: true })
              return true
            } else {
              // console.log(item.approRole + ',当前角色' + that.data.approRole + '未审批')
              that.setData({ approFlag: false })
            }
          })
          that.setData({ detailList: tempList })
          that.handleProcessList(res.data.data.applyApproveList, res.data.data.status)
        }
      }
    })
  },

  getPeopleType: function (value) {
    let tempList = ["", "总分行人员", "外员", "其他", "正式员工", "实习人员", "长期驻场人员", "驻场支持人员", "总行人员", "其他"]
    return tempList[value]
  },

  goBack: function (params) {
    if (wx.getStorageSync('source')) {
      // console.log('公众号退回')
      wx.reLaunch({
        url: '/pages/formHome/formHome'
      })
      wx.removeStorageSync('source')
    } else {
      // console.log('正常流程退回')
      wx.navigateBack({
        delta: 1,
      })
    }
  },

  approve: function () {
    let _this = this
    
    //显示等待框
    wx.showLoading({
      title: '请稍候',
      mask: true
    })
    wx.request({
      url: api.formApprove,
      method: "POST",
      data: {
        workno: wx.getStorageSync("workNo"),
        apply_id: _this.data.requestId,
        result: 1,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        wx.hideLoading()
        if (res.data.state == 1) {
          _this.setData({
            showDialog: true
          })
        } else {
          wx.showToast({
            icon: "error",
            title: res.data.message,
          })
        }
      }
    })
  },

  handleProcessList: function (applyApproveList, status) {
    let tempList = [
      {
        step: "物业办理",
      },
      {
        step: "园区审批",
      },
      {
        step: "综合管理部审批",
      },
      {
        step: "部门审批",
      },
      {
        step: "提交审批",
      },
    ]
    // 门禁卡没有园区管理办公室审批
    if (this.data.detailList.applyType == 1) {
      tempList.splice(1, 1)
    }
    let len = tempList.length
    // 被退回
    if (status == 5) {
      tempList = tempList.slice(len - applyApproveList.length, len)
      for (let i = 0; i < applyApproveList.length; i++) {
        tempList[i].date = applyApproveList[i].date
        if (i == 0) {
          tempList[i].returnReason = applyApproveList[i].returnReason
        }
      }
    } else {
      if (applyApproveList.length != len) {
        tempList = tempList.slice(len - applyApproveList.length - 1, len)
        for (let i = 0; i < applyApproveList.length; i++) {
          tempList[i + 1].date = applyApproveList[i].date
        }
      } else {
        for (let i = 0; i < applyApproveList.length; i++) {
          tempList[i].date = applyApproveList[i].date
        }
      }
    }
    if (this.data.approRole) {
      // console.log('当前角色'+this.data.approRole)
      applyApproveList.reverse().some((item, index) => {
        if (this.data.approRole == item.approRole) {
          // console.log(item.approRole + ',是当前角色')
          tempList.reverse()[index].step = '您已完成此审批'
          tempList.reverse()
          return true;
          // console.log('看看走不走这里')
        } else {
          // console.log(item.approRole + ',不是当前角色')
          if (index == (applyApproveList.length - 1)) {
            // console.log('当前下标' + index)
            tempList.reverse()[index + 1].step = '请您审批此申请'
            tempList.reverse()
          }
        }
      })
    }
    this.setData({
      processList: tempList
    })
  }
})